set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.16)

# project settings
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER  arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

project(${PROJECTNAME} C CXX ASM)

# 指定生成路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/out/${PROJECTNAME})
set(PROJECT_BINARY_DIR ${PROJECT_SOURCE_DIR}/out/${PROJECTNAME})
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/out/${PROJECTNAME})

set(USE_CXX_EXCEPTION off)  # 默认不使用c++异常

###########################
# 指定编译模式
###########################
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
  set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
      STRING "Choose the type of build." FORCE)
  # Set the possible values of build type for cmake-gui
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

###########################
# 载入相关函数
###########################
include(${PROJECT_SOURCE_DIR}/func.cmake)

###########################
# 添加项目配置
###########################
include(${PROJECT_SOURCE_DIR}/Project/${PROJECTNAME}/config.cmake)

###########################
# 添加platform配置
###########################
set(platformDir ${PROJECT_SOURCE_DIR}/platform/${PLATFORM})
include(${platformDir}/platform.cmake)

###########################
# 编译和链接参数
###########################
# Enable assembler files preprocessing
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)
# arm-none-eabi-gcc -DUSE_STDPERIPH_DRIVER     -fno-common -fmessage-length=0 -Og -g -MD -MT
add_compile_options(-ffunction-sections -fdata-sections)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -fno-rtti)
set(LINKER_SCRIPT ${linkScript})
add_link_options(-specs=nosys.specs -mthumb -mthumb-interwork)
add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
if (USE_CXX_EXCEPTION)
    add_compile_options(-fexceptions)
else()
    add_compile_options(-fno-exceptions)
    add_link_options(-specs=nano.specs)
endif ()
add_link_options(-T ${LINKER_SCRIPT})

###########################
# 定义静态库
###########################
set(staticLibGroup)

###########################
# EventDispatcher
###########################
add_subdirectory(3rd/events-dispatcher)
set(staticLibGroup ${staticLibGroup} ed4e)

###########################
# HAL库代码
###########################
add_subdirectory(Drivers)
set(staticLibGroup ${staticLibGroup} hal)

###########################
# 公共代码
###########################
add_subdirectory(Common)
set(staticLibGroup ${staticLibGroup} common)

###########################
# 项目代码
###########################
file(GLOB_RECURSE src
    "${PROJECT_SOURCE_DIR}/Project/${PROJECTNAME}/*.c"
    "${PROJECT_SOURCE_DIR}/Project/${PROJECTNAME}/*.cpp")  # 递归搜索文件
add_executable(${PROJECT_NAME}.elf ${src} ${LINKER_SCRIPT})
target_link_libraries(
    ${PROJECT_NAME}.elf PRIVATE
    ${staticLibGroup}
)
target_include_directories(
    ${PROJECT_NAME}.elf PRIVATE
    ${PROJECT_SOURCE_DIR}/Project/${PROJECTNAME}/app/inc
    ${PROJECT_SOURCE_DIR}/Project/${PROJECTNAME}/dev/inc
    ${PROJECT_SOURCE_DIR}/Project/${PROJECTNAME}/drv/inc
)

###########################
# unity
###########################
add_subdirectory(UnityTest)

message(STATUS >>> ${USE_ROM_SIZE})
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
set(ASM_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.S)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMAND ${CMAKE_OBJDUMP} -D $<TARGET_FILE:${PROJECT_NAME}.elf> > ${ASM_FILE}
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
